<template>
	<view class="home" style="background-color: #fffee8;">
		<search :showDownload="true" :download-data="data.install"></search>
		<view class="address-box" @click="goAddress" style="margin-top: 10px;">
			<view>
				<u-icon name="map"></u-icon>
			</view>
			<view>{{$t('home.deliver')}} </view>
			<view style="margin-left:5px;"><text style="font-weight: 700;">{{data.area}}</text></view>
			<view style="margin-left: 5px;">
				<u-icon name="arrow-down"></u-icon>
			</view>
		</view>
		<u-swiper @click="handleSlide" :list="data.slide" keyName="ad_code" previousMargin="20" nextMargin="20" circular
			:autoplay="true" radius="5" height="105"  bgColor="#ffffff"></u-swiper>
		<!-- 广告 -->
		<view class="adv imgPublic " v-if="data.slide2.length>0">
			<image @click="slideClick(data.slide2[0])" :src="data.slide2[0].ad_code" mode="widthFix"></image>
		</view>
		<!-- 分类 -->
		<view class="kind" style="margin-top:20px;"> 
			<!-- <swiper class="swiper" :circular='false' :indicator-dots="false" :autoplay="false"
				:display-multiple-items='4'>
				<swiper-item v-for="(item,index) in data.goodsCate" :key='index' class="swiper-item">
					<view class='kindOne'>
						<view class="kindIcon imgPublic">
							<image :src="item.image" mode="widthFix"></image>
						</view>
						<text>{{item.mobile_name}}</text>
					</view>
					<view class='kindOne'>
						<view class="kindIcon imgPublic">
							<image :src="item.image" mode="widthFix"></image>
						</view>
						<text>{{item.mobile_name}}</text>
					</view>
				</swiper-item>
			</swiper> -->
			<swiper class="cate-nav-swiper" :indicator-dots="true" :duration="300">
				<swiper-item v-for="(page_data, page_data_index) of slideDataList" :key="page_data_index">
					<u-grid :col="4" :highlight="false" :square="true" :showBorder="false">
						<u-grid-item v-for="(cate_nav, cate_nav_index) of page_data" :key="cate_nav_index"
							@click="itemClick(cate_nav)">
							<view class="cate-nav" hover-class="none" @click="itemClick(cate_nav)">
								<image class="cate-nav-image" :src="cate_nav.image" mode="aspectFit"></image>
								<text class="cate-nav-title">{{cate_nav.mobile_name}}</text>
							</view>
						</u-grid-item>
					</u-grid>
				</swiper-item>
			</swiper>
		</view>
		

		<!-- 热销商品 -->
		<view class="classify1">
			<view class="kind_title">{{$t('home.hot_sale')}}</view>
			<view class="kind_list">
				<!-- 横向滚动容器 -->
				    <scroll-view
				      scroll-x
				      :scroll-left="scrollLeft"
				      class="scroll-container"
				      @touchstart="handleTouchStart"
				      @touchmove="handleTouchMove"
				      @touchend="handleTouchEnd"
				    >
						<view class="shopOne" v-for="(item,index) in data.hot_goods" @click="goDetail(item)" :key="index">
							<view class="shopImg imgPublic">
								<image :src="item.image" mode="widthFix"></image>
								<view class="tools dFlex jBetween_aCenter">
									<view class="carIcon dFlex jCenter_aCenter" @click.stop="addFavorite(item)">
										<u-icon name="../static/images/xinyuan1.png" size='20px' v-if="item.is_collect==1"></u-icon>
										<u-icon name="../static/images/xinyuan.png" size='20px' v-else></u-icon>
									</view>
									<view class="carIcon dFlex jCenter_aCenter" @click.stop="addCart(item)">
										<u-icon name="../static/menu5_1.png" size='20px'></u-icon>
									</view>
								</view>
							</view>
							<view class='shopInfo'>
								<view class="shopName">{{item.goods_name}}</view>
								<view class="shopPrice dFlex jStart_aCenter">
									<view class='newPrice'>SAR {{item.shop_price}}</view>
									<view class='oldPrice'>{{item.market_price}}</view>
								</view>
							</view>
						</view>
					</scroll-view>
			</view>
		</view>
		<!-- 广告 -->
		<view class="adv imgPublic" v-if="data.slide3.length>0">
			<image @click="slideClick(data.slide3[0])" :src="data.slide3[0].ad_code" mode="widthFix"></image>
		</view>
		
		<!-- 限时秒杀 -->
		<view class="discount" v-if="data.flash_sale_list.length>0" style="background-color: #f2efbf;">
			<view class="titles dFlex jBetween_aCenter">
				<view class="showTit">{{$t('home.time_sale')}}</view>
				<!-- <view class='showCutDown'>
					<u-count-down :time="30 * 60 * 60 * 1000" format="HH:mm:ss"></u-count-down>
				</view> -->
				<!-- <view class="all">{{$t('home.all_discount')}}</view> -->
			</view>
			<view class="discountList dFlex jBetween_aStart fWap">
				<view class='discount_one' v-for="(item,index) in data.flash_sale_list" @click="goDetail(item)"
					:key='index'>
					<view class="shopImg imgPublic dFlex jCenter_aCenter">
						<image :src="item.image" mode="widthFix"></image>
						<view class="tools dFlex jBetween_aCenter">
							<view class="carIcon dFlex jCenter_aCenter" @click.stop="addFavorite(item)">
								<u-icon name="../static/images/xinyuan1.png" size='20px' v-if="item.is_collect==1"></u-icon>
								<u-icon name="../static/images/xinyuan.png" size='20px' v-else></u-icon>
							</view>
							<view class="carIcon dFlex jCenter_aCenter" @click.stop="addCart(item)">
								<u-icon name="../static/menu5_1.png" size='20px'></u-icon>
							</view>
						</view>
					</view>
					<view class='shopInfo'>
						<view class="shopName">{{item.goods_name}}</view>
						<view class="shopPrice dFlex jStart_aCenter">
							<view class='oldPrice'>{{item.shop_price}}</view>
							<view class='newPrice'>{{item.price}} SAR</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 广告 -->
		<view class="adv imgPublic" v-if="data.slide4.length>0">
			<image @click="slideClick(data.slide4[0])" :src="data.slide4[0].ad_code" mode="widthFix"></image>
		</view>
		<view class="classify1">
			<view class="kind_title">{{$t('home.good_recomment')}}</view>
			<view class="discountList dFlex jBetween_aStart fWap">
				<view class='discount_one' v-for="(item,index) in data.recommend_goods" @click="goDetail(item)"
					:key='index'>
					<view class="shopImg imgPublic dFlex jCenter_aCenter">
						<image :src="item.image" mode="widthFix"></image>
						<view class="tools dFlex jBetween_aCenter">
							<view class="carIcon dFlex jCenter_aCenter" @click.stop="addFavorite(item)">
								<u-icon name="../static/images/xinyuan1.png" size='20px' v-if="item.is_collect==1"></u-icon>
								<u-icon name="../static/images/xinyuan.png" size='20px' v-else></u-icon>
							</view>
							<view class="carIcon dFlex jCenter_aCenter" @click.stop="addCart(item)">
								<u-icon name="../static/menu5_1.png" size='20px'></u-icon>
							</view>
						</view>
					</view>
					<view class='shopInfo'>
						<view class="shopName">{{item.goods_name}}</view>
						<view class="shopPrice dFlex jStart_aCenter">
							<view class='oldPrice'>{{item.market_price}}</view>
							<view class='newPrice'>{{item.shop_price}} SAR</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<pageBottom></pageBottom>
		<!-- 登录弹窗 -->
		<u-popup :show="loginDialog" mode="center">
			<loginindex @close="loginDialog=false"></loginindex>
		</u-popup>
		<!-- #ifdef APP-PLUS -->
		<app-update ref="app_update" :force="false" :tabbar="true"></app-update>
		<!-- #endif -->

	</view>
</template>

<script>
	import login from '@/components/login.vue';
	import search from '@/components/search.vue';
	import pageBottom from '@/components/pageBottom.vue';
	import navSwiper from '@/components/navSwiper.vue'
	export default {
		components: {
			login,
			search,
			pageBottom
		},
		data() {
			return {
				loginDialog: false,
				data: {
					goodsCate: [],
					slide2: [],
					slide3: [],
					slide4: [],
					flash_sale_list: [],
				},
				keyword: '',
				bannerHeight:'',
				scrollLeft: 0, // 滚动位置
				  isPaused: false, // 是否暂停自动滚动
				  autoScrollInterval: null, // 自动滚动定时器
				  startX: 0, // 触摸起始位置
			};
		},
		onLoad() {
			for (var i = 0; i < 5; i++) {
				uni.setTabBarItem({
					index: i,
					text: this.$t(`tab.menu${i + 1}`),
				})
			}
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: this.$t('pages.home'),
			})
			this.init()
		},
		mounted() {
			// #ifdef APP-PLUS
			this.$refs.app_update.update(); //调用子组件 检查更新
			// #endif
			this.startAutoScroll();
		},
		beforeDestroy() {
		    this.stopAutoScroll();
		  },
		computed: {
			slideDataList() {
				if (this.data.goodsCate.length > 0) {
					const cate_count_per_page = 2 * 4;
					const page_count = this.data.goodsCate.length / cate_count_per_page;
					const slide_data_list = [];

					for (let i = 0; i < page_count; ++i) {
						slide_data_list[i] = this.data.goodsCate.slice(i * cate_count_per_page, (i + 1) *
							cate_count_per_page);
					}

					return slide_data_list;
				} else {
					return [];
				}
			}
		},
		methods: {
			// 开始自动滚动
			startAutoScroll() {
			  this.autoScrollInterval = setInterval(() => {
				if (!this.isPaused) {
				  this.scrollLeft += 1; // 每次滚动 1px
				}
			  }, 120); // 每 20ms 滚动一次
			},
			// 停止自动滚动
			stopAutoScroll() {
			  if (this.autoScrollInterval) {
				clearInterval(this.autoScrollInterval);
				this.autoScrollInterval = null;
			  }
			},
			// 触摸开始
			handleTouchStart(event) {
			  this.isPaused = true; // 暂停自动滚动
			  
			  this.startX = event.touches[0].clientX; // 记录触摸起始位置
			  
			},
			// 触摸移动
			handleTouchMove(event) {
				console.log(event);
			  
				const currentX = event.touches[0].clientX; // 当前触摸位置
				const deltaX = currentX - this.startX; // 计算滑动距离
				console.log(deltaX);
				console.log(currentX);
				this.scrollLeft -= deltaX; // 更新滚动位置
				this.startX = currentX; // 更新起始位置
			  
			},
			// 触摸结束
			handleTouchEnd() {
			  //this.isPaused = false; // 恢复自动滚动
			},
			async init() {
				const result = await this.$api('/index/home')
				console.log('result', result)
				this.data = result

			},
			handleSlide(e) {
				const item = this.data.slide[e]
				this.slideClick(item)
			},
			slideClick(item) {
				// 商品
				if (item.media_type == 3) {
					uni.navigateTo({
						url: '/pages/goodDetail?id=' + item.ad_link
					})
				}
				// 分类
				if (item.media_type == 4) {
					uni.navigateTo({
						url: '/pages/shopList?cid=' + item.ad_link
					})
				}
				// 链接
				if (item.media_type == 5) {
					uni.navigateTo({
						url: '/pages/webview?weburl=' + encodeURIComponent(item.ad_link)
					})
				}
			},
			async addFavorite(item) {
				if (!this.$user.isLogin()) {
					this.loginDialog = true
					return
				}

				await this.$tools.addFavorite(item)
				this.init()
			},
			addCart(item) {
				if (!this.$user.isLogin()) {
					this.loginDialog = true
					return
				}

				this.$tools.addCart(item)
				// this.init()
			},
			goDetail(item) {
				uni.navigateTo({
					url: './goodDetail?id=' + item.goods_id
				})
			},
			handleSearch() {
				uni.navigateTo({
					url: '/pages/shopList?keyword=' + this.keyword
				})
			},
			itemClick(item) {
				this.$cache.set('cid', item.id)
				uni.switchTab({
					url: '/pages/kind'
				})
			},
			goAddress() {
				if (!this.$user.isLogin()) {
					this.loginDialog = true
					return
				}

				uni.navigateTo({
					url: '/pages/address'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-container {
	  white-space: nowrap; /* 防止内容换行 */
	  width: 100%; /* 容器宽度 */
	  height: 200px; /* 容器高度 */
	}
	
	.home {
		padding-bottom: 50px;
	}

	.discount {
		.discountList {
			.discount_one {
				box-shadow: none;
			}
		}

		.titles {
			margin-bottom: 12px;
			width: 100%;
			padding: 12px 12px 0;
			box-sizing: border-box;

			.all {
				font-size: 12px;
				color: #909399;
			}

			.showCutDown {
				width: max-content;
				background: $main;
				padding: 6px 20px;
				border-radius: 0px 0px 5px 5px;
				position: relative;
				top: -12px;

				::v-deep.u-count-down__text {
					color: white !important;
				}
			}

			.showTit {
				font-weight: bold;
				font-size: 15px;
			}
		}

	}

	.swiper {
		display: flex;
		flex-wrap: wrap;
		height: 200px;

		.swiper-item {
			width: 25vw;
		}
	}

	.discountList {
		width: 100%;
		padding: 0 12px;
		box-sizing: border-box;

		.discount_one {
			width: 48%;
			// width: 168px;
			margin-bottom: 15px;
			overflow: hidden;
			background: white;
			border-radius: 3px;
			box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.1);

			.shopInfo {
				.shopPrice {
					width: 100%;

					.newPrice {
						font-weight: bold;
						font-size: 16px;
					}

					.oldPrice {
						text-decoration: line-through;
						margin-right: 10px;
					}
				}

				.shopName {
					width: 100%;
					overflow: hidden;
					white-space: pre-wrap;
					word-wrap: break-word;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					margin-bottom: 5px;
				}

				width:100%;
				padding:10px;
				box-sizing: border-box;
				// background:#ececec;
			}

			.shopImg {
				width: 100%;
				height: 100%;
				background: white;
				position: relative;
			}
		}
	}

	.classify1 {
		width: 100%;
		margin-top: 12px;

		.kind_title {
			width: 100%;
			padding: 0 12px;
			box-sizing: border-box;
			// color:#404553;
			//color: $main;
			font-weight: bold;
			margin-bottom: 10px;
		}

		swiper-item {
			.kindDesc {
				.kind_desc2 {
					font-size: 10px;
					margin-top: 3px;
				}

				width: 100%;
				padding: 5px;
				box-sizing: border-box;
				font-size: 12px;
			}

			display: inline-block;
			width: max-content;
			padding:0 5px;
			text-align: left;
			box-sizing:border-box;
		}

		.kind_list {
			.shopOne {
				.shopInfo {
					width: 100%;
					padding: 5px;
					box-sizing: border-box;

					.shopPrice {
						font-size: 12px;

						.oldPrice {
							margin-left: 12px;
							color: #7e8598;
							font-size: 10px;
							text-decoration: line-through;
						}
					}

					.shopName {
						font-size: 14px;
						width: 128px;
						overflow: hidden;
						white-space: pre-wrap;
						word-wrap: break-word;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						margin-bottom: 5px;
					}
				}

				.shopImg {
					width: 100%;
					position: relative;
				}

				width: 140px;
				padding: 0 6px;
				margin-right: 10px;
				box-sizing: border-box;
				display:inline-block;
				vertical-align: top;
			}

			width:100%;
			padding:0 12px;
			font-size: 16px;
			box-sizing: border-box;
			white-space: nowrap;
			overflow: hidden;
			margin-bottom: 10px;
			overflow-x: auto;

		}

		.kind_list::-webkit-scrollbar {
			height: 0;
		}
	}

	.kind {
		swiper-item {
			display: inline-block;
			width: max-content;
			padding: 0 5px;
			text-align: center;
			box-sizing: border-box;
		}

		.kindOne {
			.kindIcon {
				width: 64.5px;
				height: 64.5px;
				border-radius: 50%;
				margin: 0 auto 5px;
			}

			width:64.5px;
			display: inline-block;
			font-size:12px;
			margin-bottom: 10px;
		}

		width:calc(100% - 24px);
		margin:0 auto;
		overflow: hidden;
		overflow-x:auto;
	}

	.tools {
		width: calc(100% - 24px);
		position: absolute;
		right: 12px;
		bottom: 12px;

		.carIcon {
			width: 24px;
			height: 24px;
			padding: 2px;
			box-sizing: border-box;
			background: white;
			border-radius: 4px;
			text-align: center;
			line-height: 20px;
			box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
		}
	}

	.time {
		@include flex;
		align-items: center;

		&__custom {
			margin-top: 4px;
			width: 22px;
			height: 22px;
			background-color: $main;
			border-radius: 4px;
			/* #ifndef APP-NVUE */
			display: flex;
			/* #endif */
			justify-content: center;
			align-items: center;

			&__item {
				color: #fff;
				font-size: 12px;
				text-align: center;
			}
		}

		&__doc {
			color: $u-primary;
			padding: 0px 4px;
		}

		&__item {
			color: #606266;
			font-size: 15px;
			margin-right: 4px;
		}
	}

	.adv {
		width: calc(100% - 32px);
		margin: 12px auto;
	}

	.adv2 {
		width: calc(100% - 50px);
	}

	page {
		background: white;
	}




	.cate-nav-swiper {
		height: 280px;
	}

	.cate-nav {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
	}

	.cate-nav-image {
		width: 80.5px	;
		height: 80.5px;
		border-radius: 50%;
	}

	.cate-nav-title {
		font-size: 12px;
		color: $uni-text-color-grey;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
		height:  48px;
	}
	.u-grid{
		align-items: flex-start;
	}
	.address-box {
		display: flex;
		align-items: center;
		padding: 0 0 25rpx 20rpx;
	}
	::v-deep.u-swiper__wrapper__item__wrapper__image div{background-size:100%!important}
</style>